package cn.hu.user.service.impl;

import cn.hu.basic.commonenum.ErrorCode;
import cn.hu.basic.constants.Constant;
import cn.hu.basic.domain.dto.SmsRegisterDto;
import cn.hu.basic.exception.CustomException;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.MD5Utils;
import cn.hu.basic.util.StrUtils;
import cn.hu.user.domain.Logininfo;
import cn.hu.user.domain.User;
import cn.hu.user.mapper.LogininfoMapper;
import cn.hu.user.mapper.UserMapper;
import cn.hu.user.service.IUserService;
import cn.hu.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Hu
 * @since 2022-12-21
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void registerByPhone(SmsRegisterDto dto) {
        //判断用户是否已经存在
        User user = userMapper.findByPhone(dto.getPhone());
        if(user != null){
            throw new CustomException(ErrorCode.USER_ALREADY_EXIST.getCode());
        }
        //校验两次输入的密码是否一致
        if(!dto.getPassword().equals(dto.getConfirmPassword())){
            throw new CustomException(ErrorCode.PASSWORD_CHECK_FAILED.getCode());
        }
        //拼接短信验证码redis的key
        String smsKey = dto.getPhone()+ Constant.REGISTER_KEY;
        //redis中查询短信验证码
        String redisSmsCode = (String)redisTemplate.opsForValue().get(smsKey);
        //如果没有查询到则表示已过期
        if(redisSmsCode == null){
            throw new CustomException(ErrorCode.SMS_EXPIRED.getCode());
        }
        //校验redis中的短信验证码和输入的是否一致
        String smsCode = redisSmsCode.split(":")[0];
        if(!smsCode.equals(dto.getSmsCode())){
            throw new CustomException(ErrorCode.SMS_FAILED.getCode());
        }
        //密码加密
        String salt = StrUtils.getComplexRandomString(32);
        String password = MD5Utils.encrypByMd5(salt+dto.getPassword());
        //先存loginINfo
        Logininfo logininfo = initLoginInfo(dto, salt, password);
        logininfoMapper.add(logininfo);
        //再存user
        user = loginInfo2User(logininfo);
        userMapper.add(user);
    }

    private Logininfo initLoginInfo(SmsRegisterDto dto, String salt, String password) {
        Logininfo lf = new Logininfo();
        lf.setUsername(dto.getPhone());
        lf.setPhone(dto.getPhone());
        lf.setSalt(salt);
        lf.setPassword(password);
        lf.setType(1);
        lf.setDisable(1);
        return lf;
    }
    private User loginInfo2User(Logininfo lf){
        User user = new User();
        //bean拷贝，把相同字段的值拷贝到另一个对象的相同字段去
        BeanUtils.copyProperties(lf,user);
        user.setState(1);
        user.setCreateTime(new Date());
        user.setLogininfoId(lf.getId());
        return user;
    }
}
